<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="14">
        <el-col :span="8">
          <el-input placeholder="请输入要查询订单" v-model="queryInfo.query" class="input-with-select" size="mini" clearable @clear="getOrdersList">
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单展示表格 expand-change展开列触发事件row当前行数据-->
      <el-table :data="ordersList" border stripe size="mini">
        <el-table-column label="明细" type="expand"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格(元)" prop="order_price" width="100px"></el-table-column>
        <el-table-column label="是否付款" prop="order_pay" width="70px">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" type="success" v-if="scope.row.pay_status == '1'"> 已付款 </el-tag>
            <el-tag effect="dark" size="mini" type="danger" v-else> 未付款 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="140px"></el-table-column>
        <el-table-column label="下单时间" prop="update_time" width="140px"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改收获地址" placement="top" :enterable="false">
              <el-button icon="el-icon-edit" type="primary" size="mini" @click="showaddressDialog(scope.row.order_id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="查看物流信息" placement="top" :enterable="false">
              <el-button icon="el-icon-location" type="warning" size="mini" @click="showprogessDialog"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 7, 15, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="ordersTotal"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改收获地址" :visible.sync="addressDialogIsShow" width="50%" @close="dialogClose">
      <el-form :model="addressForm" :rules="addressFormrules" ref="addressForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="省市/县" prop="address1">
          <!-- 级联选择器 -->
          <el-cascader v-model="addressForm.address1" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogIsShow = false">取 消</el-button>
        <el-button type="primary" @click="editaddress">确 定</el-button>
      </span>
    </el-dialog>

    <!--查看物流对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="poressDialogIsShow" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressList" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../assets/js/citydata'
export default {
  name: 'orders',
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      ordersList: [],
      ordersTotal: 0,
      addressDialogIsShow: false,
      addressForm: {
        address1: [],
        address2: '',
      },
      addressFormrules: {
        address1: [{ required: true, message: '请选择省/市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
      },
      cityData,
      editOrderId: 0,
      poressDialogIsShow: false,
      progressList: [], //物流进度信息
    }
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('查询订单失败！')
      }
      res.data.goods.forEach((item) => {
        item.update_time = this.formatTime(item.update_time)
      })
      this.ordersList = res.data.goods
      this.ordersTotal = res.data.total
      //   console.log(this.ordersList, this.ordersTotal)
    },
    //把时间戳转换为我们能识别的时间方法
    formatTime(time) {
      var date = new Date(time*1000)
      const year = date.getFullYear()
      const mouth = (date.getMonth() + 1).toString().padStart(2, '0') //padStart(2,'0')取两位不够补0
      const day = date.getDate().toString().padStart(2, '0')
      const hours = date.getHours().toString().padStart(2, '0')
      const minute = date.getMinutes().toString().padStart(2, '0')
      return `${year}-${mouth}-${day}/${hours}:${minute}`
    },
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getOrdersList()
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getOrdersList()
    },
    //  getOrdersDitail(){
    //     console.log(row)
    // const{data:res} = await this.$http.get(`orders/${row.order_id}`)
    // if(res.meta.status !==200){
    //     return this.$http.error('获取订单详细失败！')
    // }
    // console.log(res.data)
    // }
    showaddressDialog(id) {
      this.editOrderId = id
      this.addressDialogIsShow = true
    },
    dialogClose() {
      this.$refs.addressForm.resetFields()
    },
    editaddress() {
      this.$refs.addressForm.validate(async (valid) => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.put(`orders/${this.editOrderId}/address`, {
          consignee_addr: this.addressForm.address1.join(',') + ',' + this.addressForm.address2,
        })
        if (res.meta.status !== 200) {
          return this.$message.error('修改地址失败！')
        }
        this.addressDialogIsShow = false
        this.getOrdersList()
        this.$message.success('修改地址成功！')
      })
    },
    async showprogessDialog() {
      const { data: res } = await this.$http.get('/kuaidi/815294206237577')
      if (res.meta.status !== 200) {
        return
      }
      // console.log(res.data)
      this.progressList = res.data
      this.poressDialogIsShow = true
    },
  },
  created() {
    this.getOrdersList()
  },
}
</script>

<style lang="less" scoped>
</style>